package cn.nexd.app.collector.widget;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.Transformation;
import android.widget.Button;
import android.widget.TextView;

import cn.nexd.app.collector.R;


/**
 * Created by xun on 2016/8/18.
 */
public class WarningDialog extends Dialog implements View.OnClickListener {
    private Animation mOverlayOutAnim;
    private AnimationSet mModalInAnim;
    private AnimationSet mModalOutAnim;
    private View dialogView;
    private String errorCode;
    private String errorMsg;
    private TextView tvErrorCode, tvErrorMsg;
    private Button btnClose;
    private OnDialogDismissListener onDialogDismissListener;

    public void setOnDialogDismissListener(OnDialogDismissListener onDialogDismissListener) {
        this.onDialogDismissListener = onDialogDismissListener;
    }

    public interface OnDialogDismissListener {
        void dismiss();
    }

    private static final String TAG = "WarningDialog";

    public WarningDialog(Context context) {
        super(context);
        mModalInAnim = (AnimationSet) OptAnimationLoader.loadAnimation(getContext(), R.anim.modal_in);
        mModalOutAnim = (AnimationSet) OptAnimationLoader.loadAnimation(getContext(), R.anim.modal_out);
        mModalOutAnim.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                dialogView.setVisibility(View.GONE);
                dialogView.post(new Runnable() {
                    @Override
                    public void run() {
                        WarningDialog.super.dismiss();
                    }
                });
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
        // dialog overlay fade out
        mOverlayOutAnim = new Animation() {
            @Override
            protected void applyTransformation(float interpolatedTime, Transformation t) {
                WindowManager.LayoutParams wlp = getWindow().getAttributes();
                wlp.alpha = 1 - interpolatedTime;
                getWindow().setAttributes(wlp);
            }
        };
        mOverlayOutAnim.setDuration(120);

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.dialog_error);
        dialogView = getWindow().getDecorView().findViewById(android.R.id.content);
        tvErrorCode = (TextView) findViewById(R.id.title_text);
        tvErrorMsg = (TextView) findViewById(R.id.content_text);
        btnClose = (Button) findViewById(R.id.dialog_btn_close);
        btnClose.setOnClickListener(this);

        setErrorCode("123");
        setErrorMsg("哦哟，忘开蓝牙了");
    }

    @Override
    protected void onStart() {
        //super.onStart();
        Log.d(TAG, "onStart: ");
        dialogView.startAnimation(mModalInAnim);
    }

    public WarningDialog setErrorCode(String errorCode) {
        this.errorCode = errorCode;
        tvErrorCode.setText(errorCode);
        return this;
    }

    public WarningDialog setErrorMsg(String errorMsg) {
        this.errorMsg = errorMsg;
        tvErrorMsg.setText(errorMsg);
        return this;
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.dialog_btn_close:
                if (isShowing()) {
                    dismiss();
                    if (onDialogDismissListener != null) {
                        onDialogDismissListener.dismiss();
                    }
                }
                break;
        }
    }
}
